home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / ROM_Kernel_Manuals / Devices / dev_examples / Terminate_Serial.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-20  |  3.3 KB  |  101 lines

  1. /*
  2.  * Terminate_Serial.c
  3.  *
  4.  * This is an example of using a termination array for reads from the serial
  5.  * device. A termination array is set up for the characters Q, E, etx (CTRL-D)
  6.  * and eot (CTRL-C).  The EOFMODE flag is set in io_SerFlags to indicate that
  7.  * we want to use a termination array by sending the SDCMD_SETPARAMS command to
  8.  * the device.  Then, a CMD_READ command is sent to the device with
  9.  * io_Length set to 25.
  10.  *
  11.  * The read will terminate whenever one of the four characters in the termination
  12.  * array is received or when 25 characters have been received.
  13.  *
  14.  * Compile with SAS C 5.10  lc -b1 -cfistq -v -y -L
  15.  *
  16.  * Run from CLI only
  17.  */
  18.  
  19. #include <exec/types.h>
  20. #include <exec/memory.h>
  21. #include <exec/io.h>
  22. #include <devices/serial.h>
  23.  
  24. #include <clib/exec_protos.h>
  25. #include <clib/alib_protos.h>
  26.  
  27. #include <stdio.h>
  28.  
  29. #ifdef LATTICE
  30. int CXBRK(void) { return(0); }  /* Disable SAS CTRL/C handling */
  31. int chkabort(void) { return(0); }  /* really */
  32. #endif
  33.  
  34.  
  35.  
  36. void main(void)
  37. {
  38. struct MsgPort  *SerialMP;          /* Define storage for one pointer */
  39. struct IOExtSer *SerialIO;         /* Define storage for one pointer */
  40.  
  41. struct IOTArray Terminators =
  42. {
  43. 0x51450403,   /* Q E etx eot */
  44. 0x03030303    /* fill to end with lowest value */
  45. };
  46.  
  47. #define READ_BUFFER_SIZE 25
  48. UBYTE ReadBuff[READ_BUFFER_SIZE];
  49. UWORD ctr;
  50.  
  51. if (SerialMP=CreatePort(0,0) )
  52.     {
  53.     if (SerialIO=(struct IOExtSer *) CreateExtIO(SerialMP,sizeof(struct IOExtSer)))
  54.         {
  55.         if (OpenDevice(SERIALNAME,0L,(struct IORequest *)SerialIO,0) )
  56.             printf("%s did not open\n",SERIALNAME);
  57.         else
  58.             {
  59.              /* Tell user what we are doing */
  60.              printf("\fLooking for Q, E, EOT or ETX\n");
  61.  
  62.              /* Set EOF mode flag
  63.               * Set the termination array
  64.               * Send SDCMD_SETPARAMS to the serial device
  65.               */
  66.              SerialIO->io_SerFlags |= SERF_EOFMODE;
  67.              SerialIO->io_TermArray = Terminators;
  68.              SerialIO->IOSer.io_Command  = SDCMD_SETPARAMS;
  69.              if (DoIO((struct IORequest *)SerialIO))
  70.                  printf("Set Params failed ");   /* Inform user of error */
  71.              else
  72.                  {
  73.                  SerialIO->IOSer.io_Length   = READ_BUFFER_SIZE;
  74.                  SerialIO->IOSer.io_Data     = (APTR)&ReadBuff[0];
  75.                  SerialIO->IOSer.io_Command  = CMD_READ;
  76.                  if (DoIO((struct IORequest *)SerialIO))     /* Execute Read */
  77.                      printf("Error: Read failed\n");
  78.                  else
  79.                      {
  80.                       /* Display all characters received */
  81.                       printf("\nThese characters were read:\n\t\t\tASCII\tHEX\n");
  82.                       for (ctr=0;ctr<SerialIO->IOSer.io_Actual;ctr++)
  83.                            printf("\t\t\t  %c\t%x\n",ReadBuff[ctr],ReadBuff[ctr]);
  84.                       printf("\nThe actual number of characters read: %d\n",
  85.                                   SerialIO->IOSer.io_Actual);
  86.                       }
  87.                  }
  88.             CloseDevice((struct IORequest *)SerialIO);
  89.             }
  90.  
  91.         DeleteExtIO((struct IORequest *)SerialIO);
  92.         }
  93.     else
  94.         printf("Error: Could not create IORequest\n");
  95.  
  96.     DeletePort(SerialMP);
  97.     }
  98. else
  99.     printf("Error: Could not create message port\n");
  100. }
  101.